<?php

namespace app\backend\controller;

use app\common\model\Shoporder as shop_orderModel;
use app\common\model\Goods as goodsModel;
use app\common\model\Reward;
use think\Controller;
use think\Request;
class GoodsOrder extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    public function dolist()
    {
        $request = request();
        $find = request()->param('order_number');
        $status1 = request()->param('status');
        $limit = $request->param('limit', 10);
        if($find && $status1){
            $list=shop_orderModel::alias('s')->join('langjin_user u','s.userid = u.id')->join('langjin_goods g','s.goods_id = g.id')->where('s.order_number',$find)->where('status',$status1-1)->field('s.order_id,s.order_number,s.userid,u.nickname,g.name,s.tprice,s.status')->order('s.order_id desc')->paginate($limit);
        }elseif($find || $status1){
            $list=shop_orderModel::alias('s')->join('langjin_user u','s.userid = u.id')->join('langjin_goods g','s.goods_id = g.id')->where('s.order_number',$find)->whereOr('status',$status1-1)->field('s.order_id,s.order_number,s.userid,u.nickname,g.name,s.tprice,s.status')->order('s.order_id desc')->paginate($limit);
        }else{
            $list = shop_orderModel::alias('s')->join('langjin_user u','s.userid = u.id')->join('langjin_goods g','s.goods_id = g.id')->field('s.order_id,s.order_number,s.userid,u.nickname,g.name,s.tprice,s.status')->order('order_id desc')->paginate($limit);
        }
        $status = ['未付款','已付款','付款后取消订单','未付款取消订单','超时未支付','拼团失败'];
        foreach($list as $key=>$value){
            $list[$key]['status1'] = $status[$value['status']];
        }
        $data = [
            'code' => 0,
            'msg' => '用户列表',
            'count' => $list->total(),
            'data' => $list->items(),
        ];
        return json($data);
    }
    public function oder_date()
    {
        $request = request();
        $order_id = request()->param('order_id');
        //echo $order_id;exit;
        if($order_id){
            $info=shop_orderModel::alias('s')->join('langjin_user u','s.userid = u.id')->join('langjin_goods g','s.goods_id = g.id')->where('s.order_id',$order_id)->field('s.order_id,s.order_number,s.reason,s.userid,u.nickname,g.name,s.tprice,s.status')->find();
            $status = ['未付款','已付款','付款后取消订单','未付款取消订单','超时未支付','拼团失败'];
            $info['status1'] = $status[$info['status']];
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    public function cancel_order(Request $request){
        if($request->isPost()) {
            $order_id = request()->param('order_id');
            $reason = request()->param('reason');
            if($reason == ''){
                $data['code'] = 1;
                $data['msg'] = lang("请填写原因");
                return $data;
            }
            $info = shop_orderModel::where('order_id',$order_id)->find();
            if($info){
                if($info['status'] == 0){
                    $res = shop_orderModel::where('order_id',$order_id)->update(['status'=>3,'reason'=>$reason]);
                    if($res){
                        $data['code'] = 0;
                        $data['msg'] = lang("取消成功");
                    }
                }
            }else{
                $data['code'] = 1;
                $data['msg'] = lang("数据异常");
            }
            return $data;
        }
    }
}
